//
//  ReportView.m
//  mendianjieke
//
//  Created by 响当当 on 10/29/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "ReportView.h"
#import "ReportHeadView.h"
#import "ReportDealHeadView.h"
#import "ReportGuestHeadView.h"

#import "ReportCell.h"

@interface ReportView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UIButton *saleButton;
@property (nonatomic, strong)UIButton *changeButton;
@property (nonatomic, strong)UIButton *guestButton;
@property (nonatomic, strong)UIView *saleLineView;
@property (nonatomic, strong)UIView *changeLineView;
@property (nonatomic, strong)UIView *guestLineView;

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)ReportHeadView *headView;
@property (nonatomic, strong)ReportDealHeadView *dealHeadView;
@property (nonatomic, strong)ReportGuestHeadView *guestHeadView;

@property (nonatomic, strong)NSMutableArray *guestArr;
@property (nonatomic, strong)NSString *segmentTag;
@end

@implementation ReportView

- (NSMutableArray *)guestArr
{
    if (!_guestArr)
    {
        _guestArr = [[NSMutableArray alloc]init];
    }
    return _guestArr;
}

- (ReportHeadView *)headView
{
    if (!_headView)
    {
        _headView = [[ReportHeadView alloc]initWithFrame:CGRectMake(0,45*kUIScaleBasePlus,SCREEN_WIDTH,474*kUIScaleBasePlus)];
    }
    return _headView;
}

- (ReportDealHeadView *)dealHeadView
{
    if (!_dealHeadView)
    {
        _dealHeadView = [[ReportDealHeadView alloc]initWithFrame:CGRectMake(0,45*kUIScaleBasePlus,SCREEN_WIDTH,474*kUIScaleBasePlus)];
    }
    return _dealHeadView;
}

- (ReportGuestHeadView *)guestHeadView
{
    if (!_guestHeadView)
    {
        _guestHeadView = [[ReportGuestHeadView alloc]initWithFrame:CGRectMake(0,45*kUIScaleBasePlus,SCREEN_WIDTH,474*kUIScaleBasePlus)];
    }
    return _guestHeadView;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,45*kUIScaleBasePlus,SCREEN_WIDTH,SCREEN_HEIGHT-TABBAR_HEIGHT-NAVIBAR_HEIGHT-20*kUIScaleBasePlus-45*kUIScaleBasePlus) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;

        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
        // 设置文字
        [header setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
//        [header setTitle:@"加载中" forState:MJRefreshStateRefreshing];
        
        // 设置字体
        header.stateLabel.font = [UIFont systemFontOfSize:15];
        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
        
        // 设置颜色
        header.stateLabel.textColor = LCHexColor(0x989898);
        header.lastUpdatedTimeLabel.textColor = LCHexColor(0x989898);
        
        // 设置刷新控件
        self.tableView.mj_header = header;
        
    }
    return _tableView;
}


- (UIButton *)saleButton
{
    if (!_saleButton)
    {
        _saleButton = [Utilties creatButtonWithFrame:CGRectMake(0,0,125*kUIScaleBasePlus, 45*kUIScaleBasePlus)
                                            textAligment:UIControlContentHorizontalAlignmentCenter
                                                    text:@"销售额"
                                                textClor:LCHexColor(0x282828)
                                               buttonBGC:[UIColor whiteColor]
                                                textFont:15*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(segmentClick:)];
        [_saleButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15*kUIScaleBasePlus]];
        _saleButton.tag = 1;
        
    }
    return _saleButton;
}

- (UIView*)saleLineView
{
    if (!_saleLineView)
    {
        _saleLineView = [[UIView alloc]initWithFrame:CGRectMake(30*kUIScaleBasePlus,35*kUIScaleBasePlus,65*kUIScaleBasePlus, 2*kUIScaleBasePlus)];
        _saleLineView.backgroundColor = LCHexColor(0xFF7A17);
    }
    return _saleLineView;
}

- (UIButton *)changeButton
{
    if (!_changeButton)
    {
        _changeButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.saleButton.frame),0,125*kUIScaleBasePlus, 45*kUIScaleBasePlus)
                                            textAligment:UIControlContentHorizontalAlignmentCenter
                                                    text:@"成交转换率"
                                                textClor:LCHexColor(0x999999)
                                               buttonBGC:[UIColor whiteColor]
                                                textFont:15*kUIScaleBasePlus
                                                  target:self
                                                method:@selector(segmentClick:)];
        _changeButton.tag = 2;
    }
    return _changeButton;
}

- (UIView*)changeLineView
{
    if (!_changeLineView)
    {
        _changeLineView = [[UIView alloc]initWithFrame:CGRectMake(150*kUIScaleBasePlus,35*kUIScaleBasePlus,75*kUIScaleBasePlus, 2*kUIScaleBasePlus)];
        _changeLineView.backgroundColor = LCHexColor(0xFF7A17);
        _changeLineView.hidden = YES;
    }
    return _changeLineView;
}

- (UIButton *)guestButton
{
    if (!_guestButton)
    {
        _guestButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.changeButton.frame),0,125*kUIScaleBasePlus, 45*kUIScaleBasePlus)
                                            textAligment:UIControlContentHorizontalAlignmentCenter
                                                    text:@"客流"
                                                textClor:LCHexColor(0x999999)
                                               buttonBGC:[UIColor whiteColor]
                                                textFont:15*kUIScaleBasePlus
                                                  target:self
                                                method:@selector(segmentClick:)];
        _guestButton.tag = 3;
    }
    return _guestButton;
}

- (UIView*)guestLineView
{
    if (!_guestLineView)
    {
        _guestLineView = [[UIView alloc]initWithFrame:CGRectMake(290*kUIScaleBasePlus,35*kUIScaleBasePlus,50*kUIScaleBasePlus, 2*kUIScaleBasePlus)];
        _guestLineView.backgroundColor = LCHexColor(0xFF7A17);
        _guestLineView.hidden = YES;
    }
    return _guestLineView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.segmentTag = @"1";
        [self addSubview:self.saleButton];
        [self addSubview:self.saleLineView];
        [self addSubview:self.changeButton];
        [self addSubview:self.changeLineView];
        [self addSubview:self.guestButton];
        [self addSubview:self.guestLineView];
        [self addSubview:self.tableView];
        
    }
    return self;
}



- (void)segmentClick:(UIButton *)sender
{
    if (sender.tag == 1)
    {
        self.saleLineView.hidden = NO;
        self.changeLineView.hidden = YES;
        self.guestLineView.hidden = YES;

        [self.saleButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15*kUIScaleBasePlus]];
        [self.changeButton.titleLabel setFont:[UIFont systemFontOfSize:15*kUIScaleBasePlus]];
         [self.guestButton.titleLabel setFont:[UIFont systemFontOfSize:15*kUIScaleBasePlus]];
        
        [self.saleButton setTitleColor:LCHexColor(0x000000) forState:UIControlStateNormal];
        [self.changeButton setTitleColor:LCHexColor(0x999999) forState:UIControlStateNormal];
        [self.guestButton setTitleColor:LCHexColor(0x999999) forState:UIControlStateNormal];
    }
    else if(sender.tag == 2)
    {
        
        self.saleLineView.hidden = YES;
        self.changeLineView.hidden = NO;
        self.guestLineView.hidden = YES;

        [self.saleButton.titleLabel setFont:[UIFont systemFontOfSize:15*kUIScaleBasePlus]];
        [self.changeButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15*kUIScaleBasePlus]];
        [self.guestButton.titleLabel setFont:[UIFont systemFontOfSize:15*kUIScaleBasePlus]];
               
        [self.saleButton setTitleColor:LCHexColor(0x999999) forState:UIControlStateNormal];
        [self.changeButton setTitleColor:LCHexColor(0x000000) forState:UIControlStateNormal];
        [self.guestButton setTitleColor:LCHexColor(0x999999) forState:UIControlStateNormal];
    }
    else
    {
        self.saleLineView.hidden = YES;
        self.changeLineView.hidden = YES;
        self.guestLineView.hidden = NO;

        [self.saleButton.titleLabel setFont:[UIFont systemFontOfSize:15*kUIScaleBasePlus]];
        [self.changeButton.titleLabel setFont:[UIFont systemFontOfSize:15*kUIScaleBasePlus]];
        [self.guestButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15*kUIScaleBasePlus]];
               
        [self.saleButton setTitleColor:LCHexColor(0x999999) forState:UIControlStateNormal];
        [self.changeButton setTitleColor:LCHexColor(0x999999) forState:UIControlStateNormal];
        [self.guestButton setTitleColor:LCHexColor(0x000000) forState:UIControlStateNormal];
    }
    
    if (self.changeTypeBlock)
    {
        self.changeTypeBlock([NSString stringWithFormat:@"%ld",(long)sender.tag]);
        self.segmentTag = [NSString stringWithFormat:@"%ld",(long)sender.tag];
    }
}

#pragma mark - delegate & dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.guestArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*kUIScaleBasePlus;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReportCell *reportCell = [tableView dequeueReusableCellWithIdentifier:@"ReportCell"];
    
    if (!reportCell)
    {
        reportCell = [[ReportCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ReportCell"];
        reportCell.selectionStyle = 0;

    }
    
    ReportGuestModel *model = [[ReportGuestModel alloc]initWithDic:self.guestArr[indexPath.row]];
    
    [reportCell getInfo:model];
    
    return reportCell;
}

- (void)getInfo:(ReportLineModel *)model tag:(NSInteger)num guestArr:(NSArray *)arr
{
    switch (num)
    {
        case 1:
        {
            [self.headView getInfo:model];
            self.tableView.tableHeaderView = self.headView;
        }
            break;
        case 2:
        {
            [self.dealHeadView getInfo:model];
            self.tableView.tableHeaderView = self.dealHeadView;
        }
            break;
        case 3:
        {
            [self.guestHeadView getInfo:model];
            self.tableView.tableHeaderView = self.guestHeadView;
        }
            break;
        default:
            break;
    }
    
    [self.guestArr removeAllObjects];
    self.guestArr = [NSMutableArray arrayWithArray:arr];
    
    [self.tableView reloadData];
}

#pragma mark - Method
- (void)loadNewData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.reloadDataBlock)
        {
            self.reloadDataBlock(self.segmentTag);
        }
        [weakSelf.tableView.mj_header endRefreshing];
    });
}



@end
